/**
 * @fileOverview 变量定义。
 * @author [mac-xiang]
 */
import * as path from "path";
import * as ts from "typescript";
import { FSWatcher } from "chokidar";
import fs from "fs";
import { parseString } from "xml2js";
// import { RemotePath, SERVER_NAME } from "../../dataBase/env.config";
// export * from "../../dataBase/env.config";
// console.log(RemotePath, SERVER_NAME);
// export const root = "D:\\Code\\novel",
export const root = path.resolve(process.cwd(), "../"),
  configPath = path.resolve(root, "dataBase/env.config.ts");
const sourceFile = ts.createProgram([configPath], {}).getSourceFile(configPath),
  ex: Record<string, string> = {};
if (sourceFile) {
  let i = 0;
  ts.forEachChild(sourceFile, node => {
    if (ts.isVariableStatement(node) && node.modifiers?.some(m => m.kind === ts.SyntaxKind.ExportKeyword)) {
      node.declarationList.declarations.forEach(decl => {
        if (ts.isIdentifier(decl.name)) {
          const varName = decl.name.text;
          let initializer = decl.initializer?.getText(sourceFile);
          if (initializer) {
            try { initializer = eval(initializer); } catch (_) { }
          }
          if (typeof varName === "string" && typeof initializer === "string") {
            ex[varName] = initializer;
          }
        }
      });
    }
  });
}
export const
  SvrIP = ex.SvrIP,
  SshPort = ex.SshPort,
  SshUsername = ex.SshUsername,
  SshKey = ex.SshKey,
  SshKeyFile = ex.SshKeyFile,
  SERVER_NAME = ex.SERVER_NAME,
  RemotePath = ex.RemotePath;
let jprt = "com.xiang", jarNamet = path.resolve(root, `api/target/api-0.0.1-SNAPSHOT.jar`);
if (fs.existsSync(path.resolve(root, "api/pom.xml"))) {
  let s = fs.readFileSync(path.resolve(root, "api/pom.xml"), "utf-8");
  parseString(s, (err, result) => {
    if (result?.project?.groupId) {
      const { groupId, name, version } = result.project;
      jprt = groupId[groupId.length - 1];
      jarNamet = `${name[0]}-${version[0]}.jar`;
    }
  });
}
export const jpr = jprt, jarName = jarNamet,
  javaPackage = `${jpr}.sql`,
  javaRoot = path.resolve(root, `api/src/main/java/${javaPackage.replace(/\./g, "/")}`),
  entityPath = path.resolve(javaRoot, "entity"),
  mapperPath = path.resolve(javaRoot, "mapper"),
  servicePath = path.resolve(javaRoot, "service"),
  serviceImplPath = path.resolve(javaRoot, "service/impl"),
  h = { watch: undefined as undefined | FSWatcher },
  remotePath = (RemotePath || "~").replace(/\/$/, ""),
  serverName = `${SERVER_NAME}`;
